

import Base64 from "./Base64"

export default class LocalStorage {

    // private static userID = "testid";

    // static setUserID(id){
    //     this.userID = ""+id;
    //     cc.log("LocalStorage  userID ",this.userID);
    // }

    static get(key) {
        if(cc.sys.platform === cc.sys.WECHAT_GAME){
            return wx.getStorageSync(key);
        }else{
            return cc.sys.localStorage.getItem(key);
        }
        
    }

    static set(key, value) {
        if(cc.sys.platform === cc.sys.WECHAT_GAME){
            return wx.setStorageSync(key,value);
        }else{
            cc.sys.localStorage.setItem(key, value);
        }        
        
    }
    static clear() {
        if(cc.sys.platform === cc.sys.WECHAT_GAME){
            return wx.clearStorageSync();
        }else{     
            cc.sys.localStorage.clear(); 
        }  
        
        
    }
    static remove(key) {
        
        if(cc.sys.platform === cc.sys.WECHAT_GAME){
            return wx.removeStorageSync(key);
        }else{  
            cc.sys.localStorage.removeItem(key);   
        }       
        
    }

    static setJson(key, value) {
        LocalStorage.set(key, JSON.stringify(value));
    }

    static getJson(key) {
        let value = this.get(key);
        if (!value) {
            return null;
        };
        return JSON.parse(value);
    }

    static getJsonBase64(key) {
        let localValue = this.get(key);
        if (!localValue) {
            return null;
        };
        let string = Base64.decode(localValue);
        if (string) {
            try {
                let value = JSON.parse(string);
                return value;
            } catch (error) {

            }
        }

        return {};

    }
    static setJsonBase64(key, value) {
        // if(value){
        LocalStorage.set(key, Base64.encode(JSON.stringify(value)));
        // }

    }
    static setBase64(key, value) {
        LocalStorage.set(key, Base64.encode(value));
    }
    static getBase64(key) {
        let localValue = this.get(key);
        if (!localValue) {
            return '';
        };
        let value = Base64.decode(localValue);
        return value;

    }

}
